package com.tripupdater.command
{
	import com.tripupdater.model.Model;
	import com.tripupdater.model.Params;
	import com.tripupdater.view.TwitterView;
	import com.tripupdater.vo.TwitterVO;
	import com.turner.caf.commands.ICommand;
	import com.turner.caf.control.CAFEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	public class GetTwitterUpdateCommand implements ICommand
	{
		private var model:Model;
		private var target:TwitterView;
		private var user:String;
		private var outputPath:String;
		
		public function GetTwitterUpdateCommand()
		{
			model= Model.getInstance();
		}

		public function execute(event:CAFEvent):void
		{
			user= event.data as String;
			var rndNumber:String= new Date().getTime().toString();
			rndNumber= rndNumber.substr(rndNumber.length - 6);
			switch(user){
				case Params.TWITTER_USER:
					outputPath= rndNumber + Params.TWITTER_OUTPUT; break;
				case Params.ALT_TWITTER_USER:
					outputPath= rndNumber + Params.ALT_TWITTER_OUTPUT; break;
			}
			target= event.viewTarget as TwitterView;
			var req:URLRequest = new URLRequest(Params.BASE_PATH + "scripts/getTwitter.php");
			req.method= URLRequestMethod.GET;
			req.data= "user=" + user + "&pass=" + Params.GENERAL_PASS + "&output=../xml/" + outputPath;
			
			var loader:URLLoader= new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.load(req);
		}
		
		private function completeHandler(e:Event):void
		{
			(e.target as URLLoader).removeEventListener(Event.COMPLETE, completeHandler);
			
			var resp:String= e.target.data.toString();
			resp= resp.substr(resp.indexOf("=")+1);
			
			var response:int= parseInt(resp);
			
			if(response == 1){
				var loader:URLLoader= new URLLoader();
				loader.addEventListener(Event.COMPLETE, parseXML);
				loader.load(new URLRequest(Params.BASE_PATH + "xml/" + outputPath));
			} else { 
				
			}
		}
		
		private function parseXML(e:Event):void
		{
			var data:XML;
			try{
				data= new XML(e.target.data as String);
			} catch(e:Error){
				target.showErrorMessage();
				return;
			}
			
			var timeline:Array= new Array();
			for each(var it:XML in data.status){
				var twit:TwitterVO= new TwitterVO();
				twit.text= it.text.toString();
				twit.date= new Date(it.date.toString());
				timeline.push(twit);
			}
			
			switch(user){
				case Params.TWITTER_USER:
					model.twitterStatusTimeline= timeline;
					break;
				case Params.ALT_TWITTER_USER:
					model.altTwitterStatusTimeline= timeline;
					break;
			}
			
			target.setTwitterTimeline();

			deleteTempFile();		
		}
		
		private function deleteTempFile():void
		{
			var req:URLRequest = new URLRequest(Params.BASE_PATH + "scripts/deleteTwitter.php");
			req.method= URLRequestMethod.GET;
			req.data= "filePath=../xml/" + outputPath;
			
			var loader:URLLoader= new URLLoader();
			loader.load(req);
		}
	}
}